/**
 * 功能：对指定目录下的文件中的内容进行正则表达式匹配
 * grep c: *.* dongge
 * 第一个参数是目录 第二个参数是文件名 第三个参数是文件中的内容
 */
/**
 * 思路：
 * 第一步：分析出路径
 * 文件查找策略：深度优先
 * 难点：回调函数如何实现递归调用
 *
 * 第二步：对文件名进行匹配
 * 第三步：对文件内容进行匹配
 */

var fs = require('fs');
var Q = require('q');
var PATH = require('path');

function listDir(path,callback){
    fs.readdir(path,function(err,files){
        if(err) throw err;
        function file(i){
            if(i>=files.length)
            {
                if(callback){
                    callback();
                }
                return;
            }
            var fileName = PATH.join(path,files[i]);
            fs.stat(fileName,function(err,stat){
                if(err) throw err;
                if(stat.isDirectory()){
                    console.log('+ '+fileName);
                    listDir(fileName,function(){
                        file(i+1);
                    });
                }else{
                    console.log('- '+fileName);
                    file(i+1);
                }

            })
        }
        file(0);
    })
}

function search(path,str){
    fs.readFile(path,function(err,data){
        /*var linePattern = /\n/g;
        var match = linePattern.exec(data);
        var i = 0;
        while(match){
            i = i + 1;
            match = linePattern.exec(data);
        }
        console.log(i);
        //行的数目不对*/
        var pattern = new RegExp(str,'g');
        //console.log(readLines(data.toString()));
        var lines = readLines(data.toString());
        lines.forEach(function (line,index){
            var result;
            while((result = pattern.exec(line))!=null){
                console.log('第'+(index+1)+'行:'+result.index);
            }
        })
        //如果i等于0
    })
}
//search("zgrep.js","var");
function readLines(data){
    return data.split(/\n/);
}
function listDir1(path,callback){
    var filePaths = [];
    function listDir(path,callback){
        fs.readdir(path,function(err,files){
            if(err) throw err;
            function file(i){
                if(i>=files.length)
                {
                    if(callback){
                        callback();
                    }
                    return;
                }
                var fileName = PATH.join(path,files[i]);
                fs.stat(fileName,function(err,stat){
                    if(err) throw err;
                    if(stat.isDirectory()){
                        //console.log('+ '+fileName);
                        listDir(fileName,function(){
                            file(i+1);
                        });
                    }else{
                        filePaths.push(fileName);
                        //console.log('- '+fileName);
                        file(i+1);
                    }

                })
            }
            file(0);
        })
    }
    if(callback){
        listDir(path,function(){
            callback(filePaths);
        })
    }else{
        listDir(path);
    }
}
function grep(path,ext,content){

    listDir1(path,function(filePaths){
        filePaths = pathFiter(filePaths,ext);
        function file(i){
            if(i >= filePaths.length){
                return;
            }
            var filePath = filePaths[i];
            search(filePath,content,function(){
                file(i+1);
            })
        }
        file(0);
    })
}
function pathFiter(filePaths,ext){
    var extArray = ext.split(/\./);
    if(extArray.length < 1){
        return [];
    }
    var extObj = {};
    extArray.forEach(function(element,i){
        extObj["."+element] = true;
    });
    return filePaths.filter(function(path){
        ext = PATH.extname(path);
        if(extObj[ext])
        {
            return true;
        }else{
            return false;
        }
    })
}
//listDir1('./',function(filePaths){
//    console.log(pathFiter(filePaths,'html'));
//})
grep('./','html','body');
function search(path,str,callback){
    fs.readFile(path,function(err,data){
        /*var linePattern = /\n/g;
         var match = linePattern.exec(data);
         var i = 0;
         while(match){
         i = i + 1;
         match = linePattern.exec(data);
         }
         console.log(i);
         //行的数目不对*/
        console.log(path);
        var pattern = new RegExp(str,'g');
        //console.log(readLines(data.toString()));
        var lines = readLines(data.toString());
        lines.forEach(function (line,index){
            var result;
            while((result = pattern.exec(line))!=null){
                console.log('第'+(index+1)+'行:'+result.index);
            }
        });
        if(callback){
            callback();
        }
    })
}
+function(){
    console.log("abc".split(/\./));
}()